package com.xingda.packing.dmss.minitor.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xingda.packing.dmss.config.ThreadLocalConfig;
import com.xingda.packing.dmss.domain.PackProductionLine;
import com.xingda.packing.dmss.enums.BoxTaskState;
import com.xingda.packing.dmss.minitor.entity.PkBoxLocation;
import com.xingda.packing.dmss.minitor.entity.PkBoxLocationRealtion;
import com.xingda.packing.dmss.minitor.entity.PkPlcLog;
import com.xingda.packing.dmss.minitor.entity.PkTask;
import com.xingda.packing.dmss.minitor.mapper.PkBoxLocationMapper;
import com.xingda.packing.dmss.minitor.mapper.PkBoxLocationRealtionMapper;
import com.xingda.packing.dmss.minitor.mapper.PkPlcLogMapper;
import com.xingda.packing.dmss.minitor.service.IPkBoxLocationRealtionService;
import com.xingda.packing.dmss.minitor.service.IPkTaskService;
import com.xingda.packing.dmss.minitor.service.IpkLogService;
import com.xingda.packing.dmss.plc.entity.PlcItem;
import com.xingda.packing.dmss.plc.entity.PlcItemVal;
import com.xingda.packing.dmss.plc.service.PlcDirectiveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author anxiaoning
 * @since 2021-07-03
 */
@Slf4j
@Service
public class PkBoxLocationRealtionServiceImpl extends ServiceImpl<PkBoxLocationRealtionMapper, PkBoxLocationRealtion> implements IPkBoxLocationRealtionService {
    @Resource
    PkBoxLocationMapper pkBoxLocationMapper;
    @Resource
    PackProductionLine packProductionLine;
    @Resource
    IPkTaskService pkTaskService;
    @Resource
    private IpkLogService ipkLogService;
    @Resource
    private ThreadLocalConfig threadLocalConfig;
    @Resource
    private PlcDirectiveService plcDirectiveService;
    @Resource
    private PkPlcLogMapper pkPlcLogMapper;
    @Value("${spring.profiles.active}")
    private String env;

    @Override
    @Transactional
    public boolean deleteBoxLocationRealtion(Integer locationId) {
        List<PkBoxLocationRealtion> locationRealtions = this.lambdaQuery().eq(PkBoxLocationRealtion::getBoxLocationId, locationId).list();
        if (CollectionUtils.isEmpty(locationRealtions)) {
            return true;
        }
        PkBoxLocationRealtion locationRealtion = locationRealtions.get(0);
        if (Objects.nonNull(locationRealtion.getTaskId())) {
            // 修改任务状态为已停止
            // 状态(1未开始 2执行中 3已完成 4已停止)
            PkTask pkTask = pkTaskService.getById(locationRealtion.getTaskId());
            if (pkTask.getState() == BoxTaskState.RUNNING.getStateId()) {
                pkTask.setState(BoxTaskState.STOP.getStateId());
                pkTask.setEndTime(new Date());
                pkTaskService.updateById(pkTask);
            }
        }
        this.clearBoxTaskByBoxLocationId(locationId);
        boolean result = this.removeById(locationRealtion.getId());
        ipkLogService.saveLog(threadLocalConfig.get());
        threadLocalConfig.remove();
        return result;
    }

    @Override
    public void deleteBoxLocationRealtion2(Integer locationId) {
        List<PkBoxLocationRealtion> locationRealtions = this.lambdaQuery().eq(PkBoxLocationRealtion::getBoxLocationId, locationId).list();
        if (CollectionUtils.isEmpty(locationRealtions)) {
            return;
        }
        PkBoxLocationRealtion locationRealtion = locationRealtions.get(0);
        if (Objects.nonNull(locationRealtion.getTaskId())) {
            // 修改任务状态为已停止
            // 状态(1未开始 2执行中 3已完成 4已停止)
            PkTask pkTask = pkTaskService.getById(locationRealtion.getTaskId());
            if (pkTask.getState() == BoxTaskState.RUNNING.getStateId()) {
                pkTask.setState(BoxTaskState.STOP.getStateId());
                pkTask.setEndTime(new Date());
                pkTaskService.updateById(pkTask);
            }
        }
        this.clearBoxTaskByBoxLocationId(locationId);
        this.removeById(locationRealtion.getId());
    }

    @Override
    public void clearBoxTaskByBoxLocationId(Integer boxLocationId) {
        int area;
        if (boxLocationId > 6) {
            area = 2;
        } else {
            area = 1;
        }
        PkBoxLocation pkBoxLocation = pkBoxLocationMapper.selectById(boxLocationId);
        if (pkBoxLocation == null) {
            log.debug("无此装箱位,id为{}", boxLocationId);
            return;
        }
        if (env.startsWith("xd-prod")) {
            PlcItem palletPhotoFinish = new PlcItem("托盘拍照类别", pkBoxLocation.getPalletPhotoFinish());
            PlcItemVal palletPhotoFinishVal = new PlcItemVal(palletPhotoFinish, false);
            plcDirectiveService.writeVals(palletPhotoFinishVal);
            List<PlcItemVal> pivList = new ArrayList<>();
            pivList.add(palletPhotoFinishVal);
            pkPlcLogMapper.insert(new PkPlcLog(area, pivList.toString(), new Date()));
        }
        packProductionLine.getProductArea().removeBox(pkBoxLocation.getBoxLocationNo());
    }
}
